//
//  NYMyExamineMsgViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/9.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyExamineMsgViewController.h"
#import "NYMyExamineMsgRequest.h"
#import "NYDelSubjectRequest.h"
#import "NYRefreshFooter.h"
@interface NYMyExamineMsgViewController ()
@property(nonatomic, strong) NYMyExamineMsgRequest *request;
@end

@implementation NYMyExamineMsgViewController


- (void)viewDidLoad
{
        [super viewDidLoad];
        
        [self loadData];
}

- (void)loadData {
        
        [self setLoading:YES animation:YES];
        [self configNimbus];
        self.request = [NYMyExamineMsgRequest requestWithParams:nil];
        
        @weakify(self);
        [[self.request pullDown] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray <NYMyExamineMsgModel *>* _Nonnull data) {
                @strongify(self);
                [self addNimbusData:data];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)addNimbusData:(NSArray <NYMyExamineMsgModel *>* _Nonnull )data
{
        [data enumerateObjectsUsingBlock:^(NYMyExamineMsgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (NULLString(obj.problem_avatar)) {
                        obj.problem_avatar = NYUserManager.avatar;
                }
                if (NULLString(obj.problem_username)) {
                        obj.problem_username = NYUserManager.nickname;
                }
        }];
        [self.tableViewModel addObjectsFromArray:data];
        [self.tableView reloadData];
}
- (void)setSuccess {
        [super setSuccess];
        [self configRefreshFooter];
}
- (void)configRefreshFooter {
        
        @weakify(self);
        self.tableView.mj_footer = [NYRefreshFooter footerWithRefreshingBlock:^{
                @strongify(self);
                [[self.request pullUp] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray <NYMyExamineMsgModel *>*  _Nonnull data) {
                        [self addNimbusData:data];
                        [data count] == 0 ? [self.tableView.mj_footer endRefreshingWithNoMoreData] : [self.tableView.mj_footer endRefreshing];
                } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }];
        }];
}

- (void)configNavigationBar:(BaseNavigationBar *)navigationBar {
        [super configNavigationBar:navigationBar];
        self.title = @"我的考试留言";
}

- (void)delClass:(NYMyExamineMsgModel *)object atIndexPath:(NSIndexPath *)indexPath
{
        [[NYDelSubjectRequest requestWithType:(NYDelObjectTypeExamineMsg) param:@{
                                                                               @"message_id" : @(object.id)
                                                                               }] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSString * _Nonnull data) {
                [NYTipClass showText:request.tipString inView:self.view];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [NYTipClass showErrorWithText:error.localizedDescription inView:self.view];
                [self.tableViewModel insertObject:object atRow:indexPath.row inSection:indexPath.section];
                [self.tableView reloadData];
        }];
}
/// MARK: - TableViewModel Delegate
- (BOOL)tableViewModel:(NIMutableTableViewModel *)tableViewModel
         canEditObject:(id)object
           atIndexPath:(NSIndexPath *)indexPath
           inTableView:(UITableView *)tableView
{
        return YES;
}
- (UITableViewRowAnimation)tableViewModel:(NIMutableTableViewModel *)tableViewModel
              deleteRowAnimationForObject:(id)object
                              atIndexPath:(NSIndexPath *)indexPath
                              inTableView:(UITableView *)tableView
{
        [self delClass:(NYMyExamineMsgModel *)object atIndexPath:(NSIndexPath *)indexPath];
        return UITableViewRowAnimationAutomatic;
}

+ (NYViewControllerNavigationBarType)navigationBarType {
        
        return NYViewControllerNavigationBarTypeBlue;
}


@end
